package com.chenjt.jvm.second.three_instruct;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 注释：
 *
 * 指令8：异常处理
 * @author chenjt
 * @date 2025/6/30 10:26
 */
public class ExceptionTest {

    public void throwZero(int i){
        if(i == 0){
            throw new RuntimeException("参数值为0");
        }
    }

    public void throwOne(int i) throws RuntimeException, IOException {
        if(i == 1){
            throw new RuntimeException("参数值为1");
        }
    }

    public void throwArithmetic(){
        int i = 10;
        int j = i / 0;
        System.out.println(j);
    }


    public void tryCatch(){
        try {
            File file = new File("");
            FileInputStream fis = new FileInputStream(file);

            String info = "hello";
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch(RuntimeException e){
            e.printStackTrace();
        }
    }


    // 思考：如下方法返回结果为什么？
    public static String func(){
        String str = "hello";
        try{
           return str;
        }finally{
            str = "chenjt";
        }
    }

    public static void main(String[] args) {
        System.out.println(func());// hello
    }



}
